﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RaProWorld
{
    public class Textbox
    {
        #region globals
        protected Game theGame;
        protected ContentManager contentManager;
        
        protected Rectangle boundaries;
        public Rectangle Boundaries
        {
            get { return boundaries; }
        }

        protected String text;
        public String Text
        {
            get { return text; }
        }

        protected SpriteFont font;

        protected Texture2D cornerBL;
        protected Texture2D cornerBR;
        protected Texture2D cornerTR;
        protected Texture2D cornerTL;
        protected Texture2D edgeL;
        protected Texture2D edgeR;
        protected Texture2D edgeT;
        protected Texture2D edgeB;
        protected Texture2D filler;
        #endregion

        #region constructors
        public Textbox()
        {
            theGame = Program.raProWorld;
            contentManager = theGame.Content;
            boundaries = new Rectangle(
                (int)(theGame.GraphicsDevice.Viewport.Width * 0.1f),
                (int)(theGame.GraphicsDevice.Viewport.Height * 0.1f),
                (int)(theGame.GraphicsDevice.Viewport.Width * 0.8f),
                (int)(theGame.GraphicsDevice.Viewport.Height * 0.3f));
            text = "";
            LoadContent();
        }

        public Textbox(String theText)
        {
            theGame = Program.raProWorld;
            contentManager = theGame.Content;
            boundaries = new Rectangle(
                (int)(theGame.GraphicsDevice.Viewport.Width * 0.1f),
                (int)(theGame.GraphicsDevice.Viewport.Height * 0.1f),
                (int)(theGame.GraphicsDevice.Viewport.Width * 0.8f),
                (int)(theGame.GraphicsDevice.Viewport.Height * 0.3f));
            text = theText;
            LoadContent();
        }

        public Textbox(Rectangle bounds)
        {
            theGame = Program.raProWorld;
            contentManager = theGame.Content;
            boundaries = bounds;
            text = "";
            LoadContent();
        }

        public Textbox(Rectangle bounds, String theText)
        {
            theGame = Program.raProWorld;
            contentManager = theGame.Content;
            boundaries = bounds;
            text = theText;
            LoadContent();
        }

        public Textbox(int x, int y, int width, int height)
        {
            theGame = Program.raProWorld;
            contentManager = theGame.Content;
            boundaries = new Rectangle(x, y, width, height);
            text = "";
            LoadContent();
        }

        public Textbox(int x, int y, int width, int height, String theText)
        {
            theGame = Program.raProWorld;
            contentManager = theGame.Content;
            boundaries = new Rectangle(x, y, width, height);
            text = theText;
            LoadContent();
        }
        #endregion

        #region methods
        public void changeWidth(int amount)
        {
            boundaries.Width = (int)MathHelper.Clamp(
                (float)boundaries.Width,
                32.0f,
                theGame.GraphicsDevice.Viewport.Width * 0.8f);
        }

        public void changeHeight(int amount)
        {
            boundaries.Height = (int)MathHelper.Clamp(
                (float)boundaries.Height,
                32.0f,
                theGame.GraphicsDevice.Viewport.Height * 0.8f);
        }

        protected void LoadContent()
        {
            font = contentManager.Load<SpriteFont>("Fonts\\TextboxFont");
            cornerBL = contentManager.Load<Texture2D>("Textbox\\cornerBL");
            cornerBR = contentManager.Load<Texture2D>("Textbox\\cornerBR");
            cornerTL = contentManager.Load<Texture2D>("Textbox\\cornerTL");
            cornerTR = contentManager.Load<Texture2D>("Textbox\\cornerTR");
            edgeL = contentManager.Load<Texture2D>("Textbox\\edgeL");
            edgeT = contentManager.Load<Texture2D>("Textbox\\edgeT");
            edgeR = contentManager.Load<Texture2D>("Textbox\\edgeR");
            edgeB = contentManager.Load<Texture2D>("Textbox\\edgeB");
            filler = contentManager.Load<Texture2D>("Textbox\\filling");
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            int edgeLengthH = boundaries.Width - 32;
            int edgeLengthV = boundaries.Height - 32;

            spriteBatch.Draw(filler,
                new Rectangle(boundaries.X + 16, boundaries.Y + 16, edgeLengthH, edgeLengthV),
                Color.White);
            
            spriteBatch.Draw(edgeT,
                new Rectangle(boundaries.X + 16, boundaries.Y, edgeLengthH, 16),
                Color.White);
            spriteBatch.Draw(edgeR,
                new Rectangle(boundaries.X + boundaries.Width - 16, boundaries.Y + 16, 16, edgeLengthV),
                Color.White);
            spriteBatch.Draw(edgeB,
                new Rectangle(boundaries.X + 16, boundaries.Y + boundaries.Height - 16, edgeLengthH, 16),
                Color.White);
            spriteBatch.Draw(edgeL,
                new Rectangle(boundaries.X, boundaries.Y + 16, 16, edgeLengthV),
                Color.White);

            spriteBatch.Draw(cornerTL,
                new Vector2(boundaries.X, boundaries.Y),
                Color.White);
            spriteBatch.Draw(cornerTR,
                new Vector2(boundaries.X + 16 + edgeLengthH, boundaries.Y),
                Color.White);
            spriteBatch.Draw(cornerBR,
                new Vector2(boundaries.X + 16 + edgeLengthH, boundaries.Y + 16 + edgeLengthV),
                Color.White);
            spriteBatch.Draw(cornerBL,
                new Vector2(boundaries.X, boundaries.Y + 16 + edgeLengthV),
                Color.White);

            spriteBatch.DrawString(font, text,
                new Vector2(boundaries.X + 16, boundaries.Y + 16),
                Color.Black);

        }
        #endregion
    }
}